package it.yuzuojian.com.controller;

import com.github.pagehelper.PageInfo;
import it.yuzuojian.com.javaBean.Tags;

import it.yuzuojian.com.service.ArticleTagsService;
import it.yuzuojian.com.service.TagsService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin/tags")
public class TagsController {
    @Autowired
    private TagsService tagsService;
    @Autowired
    private ArticleTagsService articleTagsService;

    @GetMapping("/findAll")
    @RequiresPermissions("tag_select")
    public String findAll(Model model,
                          @RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        List<Tags> tagsList = tagsService.findAll(page, pageSize);
        PageInfo<Tags> pageInfo = new PageInfo<>(tagsList);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo", pageInfo);
        if (model.getAttribute("msg") == null) {
            model.addAttribute("msg", null);
        }
        return "admin/Tags";
    }

    @GetMapping("/add/index")
    @RequiresPermissions("tag_add")
    public String addIndex(Model model) {

        model.addAttribute("tags", new Tags());
        return "admin/Tags-input";
    }

    @Transactional
    @PostMapping("/add")
    @RequiresPermissions("tag_add")
    public String add(Tags tags, Model model, RedirectAttributes attributes) {
        tags.setCount(0);
        try {
            tagsService.add(tags);
            attributes.addFlashAttribute("msg", "添加完成");
        } catch (Exception e) {
            e.printStackTrace();
            attributes.addFlashAttribute("msg", "添加失败");
            return "redirect:/admin/tags/findAll";
        }
//        model.addAttribute("tags", tags);
        return "redirect:/admin/tags/findAll";
    }

    @Transactional
    @GetMapping("/{id}/delete")
    @RequiresPermissions("tag_del")
    public String delete(RedirectAttributes attributes, @PathVariable("id") Integer id) {
        try {
            tagsService.deleteById(id);
            articleTagsService.deleteByTagId(id);
            attributes.addFlashAttribute("msg", "删除完成");
        } catch (Exception e) {
            e.printStackTrace();
            attributes.addFlashAttribute("msg", "删除失败");
            return "redirect:/admin/tags/findAll";
        }
        return "redirect:/admin/tags/findAll";
    }

    @GetMapping("/{id}/update/index")
    @RequiresPermissions("tag_update")
    public String updateIndex(@PathVariable("id") Integer id, Model model) {
        Tags tags = tagsService.findById(id);
        model.addAttribute("tags", tags);
        return "admin/tags-input";
    }

    @Transactional
    @PostMapping("/{id}/update")
    @RequiresPermissions("tag_update")
    public String update(@PathVariable("id") Integer id, Tags tags, RedirectAttributes attributes) {
        try {
            tagsService.update(tags);
            attributes.addFlashAttribute("msg", "修改成功");
        } catch (Exception e) {
            attributes.addFlashAttribute("msg", "操作失败");
        }

        return "redirect:/admin/tags/findAll";
    }
}
